﻿@{
    ViewBag.Title = "BillIndex";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<script>
    var _organizeid = "@OperatorProvider.Provider.Current().CompanyId", _typeid = "", _warehouseid = "";
    $(function () {
        InitialPage();
        GetGrid();
    });
    //初始化页面
    function InitialPage() {
        //收款状态
        $("#PaymentState").ComboBox({
            description: "==请选择==",
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 136.5);
            }, 200);
            e.stopPropagation();
        });
        //组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
                _organizeid = item.id;
                $('#btn_Search').trigger("click");
            }
        });
        $("#OrganizeId").ComboBoxTreeSetValue(_organizeid);
        //入仓类型
        $("#TypeId").ComboBoxTree({
            url: "../../SystemManage/DataItemDetail/GetDataItemTreeJson?Encode=WarehousingCategory",
            description: "==请选择==",
            height: "150px",
            click: function (item) {
                _typeid = item.id;
            }
        });
        var queryJson = {};
        if (!!_organizeid)
            queryJson = { "OrganizeId": _organizeid };
        //仓库名称
        $("#WarehouseId").ComboBoxTree({
            url: "../../ResourceManage/RS_Warehouse/GetWarehouseInfoTree?queryJson=" + JSON.stringify(queryJson),
            description: "==请选择==",
            height: "150px",
            click: function (item) {
                _warehouseid = item.id;
            }
        });
        //初始化时间
        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(- DateTime.Now.Day).ToString("yyyy-MM-dd")");
        $("#time_horizon").ComboBox({
            data: [{ "key": 2, "value": "上周" }, { "key": 1, "value": "本周" }, { "key": 4, "value": "上月" }, { "key": 3, "value": "本月" }, { "key": 5, "value": "本年" }],
            id: "key",
            description: "=选择=",
            text: "value",
            click: function (item) {
                debugger;
                var value = item.attr('data-value');
                switch (value) {
                    case "1"://本周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        break;
                    case "2"://上周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        break;
                    case "3"://本月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(- DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "4"://上月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day + 1).AddMonths(-1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "5"://本年
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.DayOfYear + 1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(12 - DateTime.Now.Month + 1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    default:
                        break;
                }
                $("#SelectedStartTime").html($("#StartTime").val());
                $("#SelectedEndTime").html($("#EndTime").val());
                $('#btn_Search').trigger("click");
            }
        });
        $("#time_horizon").ComboBoxSetValue(3);
    }
    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = $("#filter-form").GetWebControls();
        queryJson["OrganizeId"] = _organizeid;
        queryJson["TypeId"] = _typeid;
        queryJson["WarehouseId"] = _warehouseid;
        $gridTable.jqGrid({
            url: "../../ResourceManage/RS_ProductIn/GetPageList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 136.5,
            autowidth: true,
            colModel: [
                { label: '主键', name: 'Id', hidden: true },
                { label: "单据日期", name: "Date", width: 100, align: "center", formatter: "date", formatoptions: { newformat: 'Y-m-d' } },
                { label: "单据编号", name: "Code", width: 200, align: "center" },
                { label: "入仓类型", name: "TypeName", width: 150, align: "center" },
                { label: "所属仓库", name: "WarehouseId", width: 80, align: "center" },
                {
                    label: "是否审核", name: "State", width: 80, align: "center",
                    formatter: function (cellvalue) {
                        var Msg = "";
                        switch (cellvalue) {
                            case "2":
                            case 2: Msg = "<span style='Color:#009932;'>已审核</span>";
                                break;
                            case "1":
                            case 1: Msg = "<span style='Color:#F00;'>未审核</span>";
                                break;
                            case "3":
                            case 3: Msg = "审核不通过";
                                break;
                            case "4":
                            case 4: Msg = "审核不通过";
                                break;
                            default: Msg = "";
                                break;
                        }
                        return Msg;
                    }

                },
                {
                    label: "是否结算", name: "IsSettlement", width: 80, align: "center",
                    formatter: function (cellvalue) {
                        var Msg = "";
                        switch (cellvalue) {
                            case "1":
                            case 1: Msg = "是";
                                break;
                            case "0":
                            case 0: Msg = "否";
                                break;
                            default: Msg = "";
                                break;
                        }
                        return Msg;
                    }

                },
                { label: "结算结算人", name: "SettlementUserName", width: 80, align: "center" },
                {
                    label: "是否验收", name: "IsCheck", width: 80, align: "center",
                    formatter: function (cellvalue) {
                        var Msg = "";
                        switch (cellvalue) {
                            case "1":
                            case 1: Msg = "是";
                                break;
                            case "0":
                            case 0: Msg = "否";
                                break;
                            default: Msg = "";
                                break;
                        }
                        return Msg;
                    }
                },
                { label: "验收人", name: "CheckUserName", width: 200, align: "center", },
                {
                    label: "是否审核", name: "IsAudit", width: 80, align: "center", hidden: true,
                    formatter: function (cellvalue) {
                        var Msg = "";
                        switch (cellvalue) {
                            case "1":
                            case 1: Msg = "是";
                                break;
                            case "0":
                            case 0: Msg = "否";
                                break;
                            default: Msg = "";
                                break;
                        }
                        return Msg;
                    }
                },
                { label: "审核人", name: "AuditUserName", width: 200, align: "center" },
                { label: "备注", name: "Description", width: 200, align: "left" }
            ],
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100],
            pager: "#gridPager",
            sortname: 'CreateDate',
            sortorder: 'desc',
            rownumbers: true,
            sortable: true,
            shrinkToFit: false,
            gridview: true,
            ondblClickRow: function () {
                btn_detail();
            },
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
                var IsAudit = $("#gridTable").jqGridRowValue("State");
                if (IsAudit.indexOf("未审核") >= 0) {
                    $("#lr-edit").removeAttr("disabled");
                    $("#lr-delete").removeAttr("disabled");
                }
                else {
                    $("#lr-edit").attr("disabled", "disabled");
                    $("#lr-delete").attr("disabled", "disabled");
                }
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            },
            subGrid: true,
            subGridRowExpanded: function (subgrid_id, row_id) {
                var keyValue = $gridTable.jqGrid('getRowData', row_id)['Id'];
                var subgrid_table_id = subgrid_id + "_t";
                $("#" + subgrid_id).html("<table id='" + subgrid_table_id + "'></table>");
                $("#" + subgrid_table_id).jqGrid({
                    url: "../../ResourceManage/RS_ProductIn/GetDetailList",
                    postData: { keyValue: keyValue },
                    datatype: "json",
                    height: "100%",
                    colModel: [
                        { label: '商品名称', name: "Name", width: 200, align: 'left' },
                        { label: '商品编号', name: "Code", width: 200, align: 'center' },
                        { label: '规格型号', name: 'ModelNo', width: 150, align: 'center' },
                        { label: '单位', name: "Unit", width: 100, align: 'center' },
                        { label: '数量', name: 'Num', width: 80, align: 'center', formatter: 'number', formatoptions: { thousandsSeparator: "", decimalPlaces: 2 } },
                        { label: '用途', name: 'UseMemo', width: 180, align: 'center' },
                        { label: '到期时间', name: 'OverdueDate', width: 120, align: 'center', formatter: "date", formatoptions: { newformat: 'Y-m-d' } },
                        { label: '单价', name: 'UnitPrice', width: 150, align: 'center', formatter: 'number', formatoptions: { thousandsSeparator: "", decimalPlaces: 2 } },
                        { label: '金额', name: 'Price', width: 200, align: 'center', formatter: 'number', formatoptions: { thousandsSeparator: "", decimalPlaces: 2 } },
                        { label: '说明信息', name: 'Description', width: 200, align: 'left' }
                    ],
                    caption: "订单明细",
                    rowNum: "1000",
                    rownumbers: true,
                    shrinkToFit: false,
                    gridview: true,
                    hidegrid: false
                });
            }
        });

        //查询点击事件
        $("#btn_Search").click(function () {
            if (!$(".ui-filter-text").next('.ui-filter-list').is(":hidden")) {
                $(".ui-filter-text").trigger("click");
            }
            $("#SelectedStartTime").html($("#StartTime").val());
            $("#SelectedEndTime").html($("#EndTime").val());
            var queryJson = $("#filter-form").GetWebControls();
            queryJson["OrganizeId"] = _organizeid;
            queryJson["TypeId"] = _typeid;
            queryJson["WarehouseId"] = _warehouseid;
            $gridTable.jqGrid('setGridParam', {
                postData: { queryJson: JSON.stringify(queryJson) },
                page: 1
            }).trigger('reloadGrid');
        });
    }
    //新增
    function btn_add() {
        top.tablist.newTab({
            id: "Form",
            title: "新增入仓单",
            closed: true,
            replace: true,
            icon: "fa fa-file-text-o",
            url: top.contentPath + "/ResourceManage/RS_ProductIn/Form"
        });
    }
    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            $.RemoveForm({
                url: "../../ResourceManage/RS_ProductIn/RemoveForm",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").resetSelection();
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的数据项！', 0);
        }
    }
    //审核
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");

        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "AuditForm",
                title: '审核',
                url: '/ResourceManage/RS_ProductIn/AuditForm?keyValue=' + keyValue,
                width: "450px",
                height: "350px",
                callBack: function (iframeId) {
                    top.frames["AuditForm"].AcceptClick();
                },
            })
        }
    }
    //详情
    function btn_detail() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        var Code = $("#gridTable").jqGridRowValue("Code");
        if (checkedRow(keyValue)) {
            top.tablist.newTab({
                id: "BillForm",
                title: "入仓单" + Code + "】",
                closed: true,
                replace: true,
                icon: "fa fa-file-text-o",
                url: top.contentPath + "/ResourceManage/RS_ProductIn/Form?keyValue=" + keyValue
            });
        }
    }
</script>
<div class="titlePanel">
    <div class="title-search">
        <table>
            <tr>
                <td>筛选条件</td>
                <td><div id="OrganizeId" type="selectTree" class="ui-select " style="width:200px;"></div></td>
                <td style="padding-left: 10px;">
                    <div class="ui-filter" style="width: 200px;">
                        <div class="ui-filter-text">
                            <strong id="SelectedStartTime">@DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToString("yyyy-MM-dd")</strong> 至 <strong id="SelectedEndTime">@DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")</strong>
                        </div>
                        <div class="ui-filter-list" style="width: 350px;">
                            <table class="form" id="filter-form">
                                <tr>
                                    <th class="formTitle">单据日期：</th>
                                    <td class="formValue">
                                        <input id="Category" type="hidden" value="1" />
                                        <div style="float: left; width: 45%;">
                                            <input id="StartTime" readonly type="text" value="@DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd")" class="form-control input-wdatepicker" onfocus="WdatePicker({maxDate:'%y-%M-%d'})">
                                        </div>
                                        <div style="float: left; width: 10%; text-align: center;">至</div>
                                        <div style="float: left; width: 45%;">
                                            <input id="EndTime" readonly type="text" value="@LeSoft.Util.Time.GetToday()" class="form-control input-wdatepicker" onfocus="WdatePicker({maxDate:'%y-%M-%d'})">
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitle">单据编号：</td>
                                    <td class="formValue">
                                        <input id="Code" type="text" class="form-control">
                                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitle">入仓类型：</td>
                                    <td class="formValue">
                                        <div id="TypeId" type="selectTree" class="ui-select "></div>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="formTitle">所属仓库：</td>
                                    <td class="formValue">
                                        <div id="WarehouseId" type="selectTree" class="ui-select "></div>
                                    </td>
                                </tr>

                            </table>
                            <div class="ui-filter-list-bottom">
                                <a id="btn_Reset" class="btn btn-default">&nbsp;重&nbsp;&nbsp;置</a>
                                <a id="btn_Search" class="btn btn-primary">&nbsp;查&nbsp;&nbsp;询</a>
                            </div>
                        </div>
                    </div>
                </td>
                <td style="padding-left: 10px;">
                    <div id="time_horizon" type="selectTree" class="ui-select" style="width: 60px;"></div>
                </td>
                @*<td style="padding-left: 10px;">
                        <div id="time_horizon" class="btn-group">
                            <a class="btn btn-default" data-value="1">今天</a>
                            <a class="btn btn-default" data-value="2">近7天</a>
                            <a class="btn btn-default active" data-value="3">近1个月</a>
                            <a class="btn btn-default" data-value="4">近3个月</a>
                        </div>
                    </td>*@
            </tr>
        </table>
    </div>
    <div class="toolbar">
        <div class="btn-group">
            <a id="lr-replace" class="btn btn-default" onclick="reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
            <a id="lr-add" class="btn btn-default" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
            <a id="lr-detail" class="btn btn-default" onclick="btn_detail()"><i class="fa fa-list-alt"></i>&nbsp;详细</a>
            <a id="lr-delete" class="btn btn-default" disabled onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
            <a id="lr-edit" class="btn btn-default" disabled onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;审核</a>
            <div class="btn-group">
                <a id="lr-more" class="btn btn-default" data-toggle="dropdown"><i class="fa fa-reorder"></i>&nbsp;更多操作<span class="caret"></span></a>
            </div>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>
</div>
<div class="gridPanel">
    <table id="gridTable"></table>
    <div id="gridPager"></div>
</div>